﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.WinKStart.Extension;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using System.Reflection;

namespace WMSSoft.WinKStart.ViewModel
{
    public class Setting_NewEditPanelViewModel : CSettingsBaseViewModel
    {
        WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel _Panel = null;
        List<KeyValueAssignment> _AvailableExpandStates = null;
        List<KeyValueAssignment> _DefaultExpandableState = null;
        List<KeyValueAssignment> _DExpandStatesToOtherPanelState = null;
        List<KeyValueAssignment> _DataSourceItems = null;
        List<KeyValueAssignment> _NavigationStates = null;
        List<KeyValueAssignment> _ContextMenuStates = null;
        List<KeyValueAssignment> _SortStates = null;
        bool _ChooseFilterIsActive = false;
        bool _CanEditPanelID = true;
        bool _DataChanged = false;
        string _PanelCaption = "";
        string _PanelID = "";
        string _SelectedExpandableState = "false";
        string _SelectedDefaultExpandableState = "Expanded";
        string _SelectedStateToOtherPanelState = "false";
        string _SelectedDataSource = "";
        string _SelectedFilter = "";
        string _NavigationState = "false";
        string _UseContextMenu = "false";
        string _SortItem = "false";
        int _MaxVisiableItems = 5;

        #region Initialize
        public Setting_NewEditPanelViewModel()
        {
            base.ViewName = LanguageStrings.GetText("New Panel");
            PropertyChanged += Setting_NewEditPanelViewModel_PropertyChanged;

            ViewModel_OkClick += Setting_NewEditPanelViewModel_ViewModel_OkClick;
            ViewModel_CloseClick += Setting_NewEditPanelViewModel_ViewModel_CloseClick;

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("..."), new CDelegatingCommand(Delegate => this.Cmd_Exec_ChooseFilter())));

            _Panel = new WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel();
            _AvailableExpandStates = Meta_Regions.GetRegionAvailableExpandStates();
            _DefaultExpandableState = Meta_Regions.GetDefaultExpandStates();
            _DExpandStatesToOtherPanelState = Meta_Regions.GetExpandStatesIfChangeOtherState();
            _DataSourceItems = Meta_Regions.GetDataSources();
            _NavigationStates = Meta_Regions.GetNavigationStates();
            _ContextMenuStates = Meta_Regions.GetContextMenuStates();
            _SortStates = Meta_Regions.GetSortStates();
        }

        public Setting_NewEditPanelViewModel(WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel Panel)
        {
            base.ViewName = LanguageStrings.GetText("Edit Panel");
            PropertyChanged += Setting_NewEditPanelViewModel_PropertyChanged;

            CanEditPanelID = false;

            ViewModel_OkClick += Setting_NewEditPanelViewModel_ViewModel_OkClick;
            ViewModel_CloseClick += Setting_NewEditPanelViewModel_ViewModel_CloseClick;

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("..."), new CDelegatingCommand(Delegate => this.Cmd_Exec_ChooseFilter())));

            _Panel = Panel;
            _AvailableExpandStates = Meta_Regions.GetRegionAvailableExpandStates();
            _DefaultExpandableState = Meta_Regions.GetDefaultExpandStates();
            _DExpandStatesToOtherPanelState = Meta_Regions.GetExpandStatesIfChangeOtherState();
            _DataSourceItems = Meta_Regions.GetDataSources();
            _NavigationStates = Meta_Regions.GetNavigationStates();
            _ContextMenuStates = Meta_Regions.GetContextMenuStates();
            _SortStates = Meta_Regions.GetSortStates();
            PanelCaption = _Panel.Caption;
            PanelID = _Panel.PanelID;
            SelectedExpandableState = _Panel.CanCollapsExpand.ToString().ToLower();
            DefaultExpandableState = _Panel.DefaultOpenedPanelStyle.ToString(); //TODO
            SelectedExpandableStateToOtherExpand = _Panel.CloseIfOpenOtherPanel.ToString().ToLower();
            SelectedDataSource = _Panel.DataSource;
            SelectedFilter = _Panel.Filter;
            NavigationState = _Panel.CanNavigate.ToString().ToLower();
            SortItems = _Panel.SortItems.ToString().ToLower();
            MaxVisibleItems = _Panel.MaxViewedElements;
            //UseContextMenu = 

            _DataChanged = false;
        }

        #endregion

        void Setting_NewEditPanelViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "SelectedExpandableState")
                {
                    if (SelectedExpandableState == "false")
                        DefaultExpandableState = "Expanded";
                }
                else if (e.PropertyName == "DefaultExpandableState")
                {
                    if (SelectedExpandableState == "false" && DefaultExpandableState == "Collapsed")
                    {
                        DefaultExpandableState = "Expanded";
                    }
                }
                else if (e.PropertyName == "SelectedDataSource")
                {
                    if (SelectedDataSource == "")
                        ChooseFilterIsEnabled = false;
                    else
                        ChooseFilterIsEnabled = true;
                }

                _DataChanged = true;
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error while Processing Property - Changing", Ex, true, EMessageTypes.Error);
            }
        }


        #region Properties

        public WMSSoft.Lib.WinKStart.Model.MenuConfig.MinimizeablePanel Panel
        {
            get { return _Panel; }
        }

        public CSingleCommandViewModel Cmd_ChooseFilter
        {
            get { return m_oCommands[3]; }
        }

        public List<KeyValueAssignment> ExpandableStates
        {
            get { return _AvailableExpandStates; }
            set
            {
                _AvailableExpandStates = value;
                OnPropertyChanged("ExpandableStates");
            }
        }

        public List<KeyValueAssignment> DefaultExpandableStates
        {
            get { return _DefaultExpandableState; }
            set
            {
                _DefaultExpandableState = value;
                OnPropertyChanged("DefaultExpandableStates");
            }
        }

        public List<KeyValueAssignment> EpandStatesIfOtherPanelExpand
        {
            get { return _DExpandStatesToOtherPanelState; }
            set
            {
                _DExpandStatesToOtherPanelState = value;
                OnPropertyChanged("EpandStatesIfOtherPanelExpand");
            }
        }

        public List<KeyValueAssignment> DataSourceItems
        {
            get { return _DataSourceItems; }
            set
            {
                _DataSourceItems = value;
                OnPropertyChanged("DataSourceItems");
            }
        }

        public List<KeyValueAssignment> NavigationStates
        {
            get { return _NavigationStates; }
            set
            {
                _NavigationStates = value;
                OnPropertyChanged("NavigationStates");
            }
        }

        public List<KeyValueAssignment> ContextMenuStates
        {
            get { return _ContextMenuStates; }
            set
            {
                _ContextMenuStates = value;
                OnPropertyChanged("ContextMenuStates");
            }
        }

        public List<KeyValueAssignment> SortStates
        {
            get { return _SortStates; }
            set
            {
                _SortStates = value;
                OnPropertyChanged("SortStates");
            }
        }

        public string PanelCaption
        {
            get { return _PanelCaption; }
            set
            {
                _PanelCaption = value;
                OnPropertyChanged("PanelName");
            }
        }

        public string PanelID
        {
            get { return _PanelID; }
            set
            {
                _PanelID = value;
                OnPropertyChanged("PanelID");
            }
        }

        public string SelectedExpandableState
        {
            get { return _SelectedExpandableState; }
            set
            {
                _SelectedExpandableState = value;
                OnPropertyChanged("SelectedExpandableState");
            }
        }

        public string DefaultExpandableState
        {
            get { return _SelectedDefaultExpandableState; }
            set
            {
                _SelectedDefaultExpandableState = value;
                OnPropertyChanged("DefaultExpandableState");
            }
        }

        public string SelectedExpandableStateToOtherExpand
        {
            get { return _SelectedStateToOtherPanelState; }
            set
            {
                _SelectedStateToOtherPanelState = value;
                OnPropertyChanged("SelectedExpandableStateToOtherExpand");
            }
        }

        public string SelectedDataSource
        {
            get { return _SelectedDataSource; }
            set
            {
                _SelectedDataSource = value;
                OnPropertyChanged("SelectedDataSource");
            }
        }

        public string SelectedFilter
        {
            get { return _SelectedFilter; }
            set
            {
                _SelectedFilter = value;
                OnPropertyChanged("SelectedFilter");
            }
        }

        public string NavigationState
        {
            get { return _NavigationState; }
            set
            {
                _NavigationState = value;
                OnPropertyChanged("NavigationState");
            }
        }

        public string UseContextMenu
        {
            get { return _UseContextMenu; }
            set
            {
                _UseContextMenu = value;
                OnPropertyChanged("UseContextMenu");
            }
        }

        public string SortItems
        {
            get { return _SortItem; }
            set
            {
                _SortItem = value;
                OnPropertyChanged("SortItems");
            }
        }

        public bool ChooseFilterIsEnabled
        {
            get { return _ChooseFilterIsActive; }
            set
            {
                _ChooseFilterIsActive = value;
                OnPropertyChanged("ChooseFilterIsEnabled");
            }
        }
        public int MaxVisibleItems
        {
            get { return _MaxVisiableItems; }
            set
            {
                _MaxVisiableItems = value;
                OnPropertyChanged("MaxVisibleItems");
            }
        }

        public bool CanEditPanelID
        {
            get { return _CanEditPanelID; }
            set
            {
                _CanEditPanelID = value;
                OnPropertyChanged("CanEditPanelID");
            }
        }

        #endregion

        #region Command Procedures 

        void Setting_NewEditPanelViewModel_ViewModel_CloseClick(object sender, EventArgs e)
        {
            try
            {
                Setting_NewEditPanel parentDlg = null;
                if ((Setting_NewEditPanel)((CDelegatingCommand)Cmd_CloseClick.Command).CommandParameter != null)
                {
                    parentDlg = (Setting_NewEditPanel)((CDelegatingCommand)Cmd_CloseClick.Command).CommandParameter;

                    if (_DataChanged == true)
                    {
                        if (System.Windows.MessageBox.Show(QuestionMessages.QUESTION_CLOSE_DIALOG__LOSE_CHANGES, "Question",
                            System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.No)
                            return;
                    }

                    if (parentDlg != null)
                        parentDlg.Close();
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't Close Dialog!"), Ex, EMessageTypes.Error);
            }      
        }

        void Setting_NewEditPanelViewModel_ViewModel_OkClick(object sender, EventArgs e)
        {
            try
            {
                Setting_NewEditPanel parentDlg = null;
                if ((Setting_NewEditPanel)((CDelegatingCommand)Cmd_OkClick.Command).CommandParameter != null)
                {
                    parentDlg = (Setting_NewEditPanel)((CDelegatingCommand)Cmd_OkClick.Command).CommandParameter;

                    //Apply Data
                    _Panel.Caption = PanelCaption;
                    _Panel.PanelID = PanelID;
                    if (SelectedExpandableState == "false" && DefaultExpandableState == "Collapsed")
                    {
                        System.Windows.MessageBox.Show(LanguageStrings.GetText(InfoMessages.INFO_NEW_EDIT_PANEL__INVALID_DEFAULT_EXPANDABLE_STATE, EStringTypes.stMessage), 
                                                       LanguageStrings.GetText("Info"), 
                                                       System.Windows.MessageBoxButton.OK );
                        return;
                    }
                    if (PanelCaption.Length > 0)
                        _Panel.Caption = PanelCaption;
                    else
                    {
                        System.Windows.MessageBox.Show(LanguageStrings.GetText(InfoMessages.INFO_NEW_EDIT_PANEL__INVALID_PANEL_CAPTION, EStringTypes.stMessage),
                                                       LanguageStrings.GetText("Info"), 
                                                       System.Windows.MessageBoxButton.OK);
                        return;
                    }

                    if (PanelID.Length > 0)
                        _Panel.PanelID = PanelID;
                    else
                    {
                        System.Windows.MessageBox.Show(LanguageStrings.GetText(InfoMessages.INFO_NEW_EDIT_PANEL__INVALID_PANELID, EStringTypes.stMessage),
                                                       LanguageStrings.GetText("Info"), 
                                                       System.Windows.MessageBoxButton.OK);
                        return;
                    }

                    _Panel.CanCollapsExpand = bool.Parse(SelectedExpandableState);
                    _Panel.DefaultOpenedPanelStyle = (PanelOpenedStyle)Enum.Parse(typeof(PanelOpenedStyle), DefaultExpandableState);
                    _Panel.CloseIfOpenOtherPanel = bool.Parse(SelectedExpandableStateToOtherExpand);
                    _Panel.DataSource = SelectedDataSource;
                    _Panel.Filter = SelectedFilter;
                    _Panel.CanNavigate = bool.Parse(NavigationState);
                    _Panel.SortItems = bool.Parse(SortItems);
                    _Panel.MaxViewedElements = _MaxVisiableItems;

                    if (parentDlg != null)
                    {
                        parentDlg.DialogResult = true;
                        parentDlg.Close();
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't Close Dialog!"), Ex, EMessageTypes.Error);
            }            
        }

        void Cmd_Exec_ChooseFilter()
        {
            try
            {
                ChooseFilterDlgViewModel tmpChooseDlgViewModel = new ChooseFilterDlgViewModel(SelectedDataSource, SelectedFilter);
                ChooseFilterDlg tmpChooseFilterDlg = new ChooseFilterDlg();
                tmpChooseFilterDlg.DataContext = tmpChooseDlgViewModel;
                if (tmpChooseFilterDlg.ShowDialog() == true)
                {
                    SelectedFilter = ((ChooseFilterDlgViewModel)tmpChooseFilterDlg.DataContext).ActiveFilter;
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error while choosing Filter!", Ex, true, EMessageTypes.Error);
            }
        }

        #endregion
    }
}
